package com.Tread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @Author:moutainMan
 * @Description:    自定义单例线程池
 * @Authore: 罗蔺
 */
public class SingleThreadPool {
    //懒汉式加载
    private  static volatile  ThreadPoolExecutor getThreadPoolExecutor = null;
    //核心线程数
    private static final int coreThread = 8;
    //最大线程数
    private static final int maxSizeThread = 20;
    //线程活跃时间
    private static final int keepActiveTime = 60;

    private static final TimeUnit timeUnit = TimeUnit.SECONDS;
    private static ThreadPoolExecutor  threadPoolExecutor = new ThreadPoolExecutor(coreThread,maxSizeThread,keepActiveTime,timeUnit,new LinkedBlockingQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy());

    private static synchronized void create(){
        if(threadPoolExecutor == null){
          SingleThreadPool.getThreadPoolExecutor = new ThreadPoolExecutor(coreThread,maxSizeThread,
                  keepActiveTime,timeUnit,new LinkedBlockingQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy());
        }
    }

    public static ThreadPoolExecutor getInstance(){
        if (threadPoolExecutor == null)
            create();
        return threadPoolExecutor;
    }





    public static void main(String[] args) {

        ThreadPoolExecutor executor = SingleThreadPool.getInstance();

        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(1000);
                System.out.println("异步线程");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
//        thread.run();
        thread.start();
        System.out.println("sdfs");
        LinkedBlockingQueue<Runnable> queue = (LinkedBlockingQueue<Runnable>) executor.getQueue();



    }


}
